<script setup>
import {onMounted, reactive, ref} from 'vue'
import router from '@/router'
import {pageListEnrollContestService} from "@/api/contest.js";

// 我的竞赛列表
const myContestList = ref([])

const totalSize = ref(0)
const params = reactive({
  current: 1,
  size: 10,
})

// 分页
function handleSizeChange() {
  params.current = 1
  getContestList()
}

function handleCurrentChange() {
  getContestList()
}

async function getContestList() {
  const result = await pageListEnrollContestService(params.current, params.size)
  myContestList.value = result.data.records
  totalSize.value = result.data.total
}

//获取报名竞赛列表
onMounted(() => {
  getContestList()
})

function isHistoryExam(exam) {
  const now = new Date();
  return new Date(exam.endTime) < now;
}

const isNotStart = (exam) => {
  const now = new Date(); //当前时间
  return new Date(exam.beginTime) > now;
}

const goBack = () => {
  router.go(-1)
}
</script>

<template>
  <div class="my-exam-page">
    <div class="exam-list-block">
      <div class="exam-list-header">
        <span class="ex-title">我的竞赛</span>
        <span class="exam-list-back" @click="goBack">返回</span>
      </div>
      <div class="exam-list-content" v-for="(exam, index) in myContestList" :key="index">
        <img src="@/assets/images/myContest.png" class="image"/>
        <div class="exam-content">
          <div class="title">
            {{ exam.title }}
          </div>
          <div class="date"><span>比赛时间:</span> {{ exam.beginTime }} ~ {{ exam.endTime }}</div>
        </div>
        <div>
          <div class="exam-end-lable-list" v-if="isHistoryExam(exam)">
            <span class="exam-end-lable">已完赛</span>
            <el-button class="exam-rank-lable" type="text" @click="togglePopover(exam.examId)">结果详情</el-button>
          </div>
          <div class="exam-status-lable exam-end-lable-list" v-else-if="isNotStart(exam)">未开赛</div>
          <div class="exam-end-lable-list" v-else>
            <span class="exam-begin-lable">已开赛</span>
            <el-button class="exam-rank-lable" type="text" plain @click="goExam(exam)">开始答题</el-button>
          </div>
        </div>
      </div>
      <div class="my-exam-pagination">
        <!-- 增加分页展示器 -->
        <el-pagination background layout="total, sizes, prev, pager, next, jumper"
                       :total="totalSize"
                       v-model:current-page="params.current"
                       v-model:page-size="params.size"
                       :page-sizes="[1, 5, 10, 15, 20]"
                       @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"/>
      </div>
    </div>
  </div>
</template>

<style lang="scss">
.my-exam-page {
  max-width: 1550px;
  margin: 20px auto;
  background-color: #ffffff;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border-radius: 15px;
  padding: 20px;
}

.exam-list-block {
  width: 100%;
  display: flex;
  flex-direction: column;
}

.exam-list-header {
  background-color: rgba(250, 250, 250, 1);
  border-radius: 10px;
  width: 100%;
  height: 60px;
  font-size: 20px;
  text-indent: 20px;
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  box-shadow: 0px 2px 6px rgba(0, 0, 0, 0.05);

  .ex-title {
    font-family: 'PingFang SC', sans-serif;
    font-weight: 600;
    font-size: 20px;
    color: #333333;
  }

  .exam-list-back {
    cursor: pointer;
    color: #666666;
    font-size: 18px;
    margin-left: auto;
    padding-right: 30px;
    transition: color 0.3s ease;

    &:hover {
      color: #222222;
    }
  }
}

.exam-list-content {
  height: 110px;
  width: 100%;
  background: #ffffff;
  border-radius: 10px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  margin-top: 10px;
  justify-content: space-between;
  padding: 10px 20px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
  cursor: pointer;

  &:hover {
    box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.1);
    transform: translateY(-2px);
    transition: all 0.3s ease;
  }

  img {
    width: 50px;
    height: 50px;
    margin: 0 20px;
    border-radius: 10px;
  }
}

.exam-list-content .exam-content {
  max-width: calc(100% - 320px);
  width: 100%;
}

.exam-list-content .title {
  font-weight: bold;
  font-size: 16px;
  color: #222222;
  margin-bottom: 10px;
}

.exam-list-content .date {
  font-weight: 400;
  color: #666666;
  line-height: 20px;
  font-size: 14px;

  span {
    color: #999999;
  }
}

.exam-list-content .exam-status-lable {
  color: #999999;
  font-weight: bold;
  font-size: 14px;
}

.exam-end-lable-list {
  padding-right: 20px;
  min-width: 228px;
  text-align: right;

  .exam-end-lable {
    margin-right: 20px;
    font-family: 'Microsoft YaHei', sans-serif;
    font-size: 14px;
    color: #999999;
    background: #f9f9f9;
    padding: 5px 10px;
    border-radius: 10px;
  }

  .exam-begin-lable{
    margin-right: 20px;
    font-family: 'Microsoft YaHei', sans-serif;
    font-size: 14px;
    color: #58ed2b;
    background: #eafdec;
    padding: 5px 10px;
    border-radius: 10px;
  }

  .exam-rank-lable {
    font-size: 14px;
    color: #007BFF;
    cursor: pointer;
    transition: color 0.3s ease;

    &:hover {
      color: #0056b3;
    }
  }
}

.my-exam-pagination {
  width: 100%;
  display: flex;
  justify-content: flex-end;
  padding: 20px 0;
  border-top: 1px solid #f0f0f0;
  margin-top: 20px;
}

.el-pagination {
  background: #fff;
  border-radius: 5px;
  padding: 10px;
}
</style>